/*******************************************************************************
*  Project      : Minigate Golfin                                 
*  Program ID   : pop_sender.js
*  Description  : 발송자목록 
*
********************************************************************************
*  Program History                                                            
*  Date        Author    Description                                          
*  ----------  --------  -------------------------------------------------------- 
*  2012-05-08  김효순    Created.  
*  2012-05-08  김효순    Complete.  
*******************************************************************************/

jQuery(function($){
	function clsEmailList()
	{
		this._HTMLDom = null;
		this._ERRMSG = {
			"NODATA": "선택할 이메일이 없습니다."
			, "NOCHECK": "추가할 이메일을 선택해 주십시요."
		}
		this.errMsg = null;
		this.listData = null;
		
		this._init();
	}
	
	clsEmailList.prototype = {
		_init: function(){
			var $layer = $("div.pw_area>ul.pop_area_choice");
			
			this._HTMLDom = {
				"$layer": $layer
			}
			
			this.setList([]);
		}
		, $getLayer: function(){
			return this._HTMLDom["$layer"];
		}
		, setList: function(thisData){
			this.listData = thisData;
			this.display();
		}
		, display: function(){
			var $layer = this.$getLayer();
			var thisClass = this;
			
			$layer.empty();
			for(var i=0;i<this.listData.length;i++){
				$layer.append('<li><label><input type="checkbox" id="idEmail' + i + '" />' + this.listData[i]["이메일"]+ '<a href="#"><img src="../images/btn/btn_x03.gif" alt="삭제" /></a></label></li>');
			}
		}
		, getValue: function(){
			var $layer = this.$getLayer();
			var thisClass = this;
			
			var r = "";
			$layer.find("input:checkbox:checked").each(function(){
				var n = $layer.find("input:checkbox").index($(this));
				
				if(r) r += ";";
				r += thisClass.listData[n]["이메일"];
			});
			
			return r;
		}
		, getEmail: function(n){
			return this.listData[n]["이메일"];
		}
		, getError: function(){
			if(this.errMsg) return this._ERRMSG[this.errMsg];
			return null;
		}
		, valid: function(){
			var $layer = this.$getLayer();
			
			this.errMsg = null;
			if(!this.listData.length){
				this.errMsg = "NODATA";
				return false;
			}else if(!this.getValue()){
				this.errMsg = "NOCHECK";
				return false;
			}
			
			return true;
		}
		, spinStart: function(){
			var $layer = this.$getLayer();
			
			var w = $layer.width();
			var h = $layer.height();
			$layer.hide();
			if($layer.prev("div#tmpSpin").length == 0){
				$layer.before("<div id='tmpSpin' style='position:relative; width:" + w + "px; height:" + h + "px;' />");
			}
			$layer.prev("div#tmpSpin").show().spin();
		}
		, spinEnd: function(){
			var $layer = this.$getLayer();
			
			$layer.show();
			$layer.prev("div#tmpSpin").hide().spin(false);
		}
	}
	
	function clsComm()
	{
		
		this._init();
	}
	
	clsComm.prototype = {
		_init: function(){

		}
		// 이메일 리스트
		, getEmailList: function(arg){
			arg = arg || {}
			arg["콜백"] = arg["콜백"] || function(){}
			
			//ajax
			var serviceName = "reservEmailPop";
			var options = {
				
			}
			_gf.ajax({
				"sid": "RS001"
				, "servicename": serviceName
			}, options, function(ret){
				var res = {
					"결과코드": ret["msp"]["contents"]["serviceResult"]
					, "메시지": _gf.getResultMsg(ret)
					, "결과값": []
				}
				
				var items = ret["msp"]["contents"]["items"];
				if(items){
					if(!items.length) items = new Array(items);
					for(var i=0;i<items.length;i++){
						var obj = items[i];
						res["결과값"].push({
							"이메일": _util.nvl(obj["email"])
						});
					}
				}
				
				arg["콜백"].apply(null, [res]);
			});
		}
		// 삭제
		, emailDelete: function(arg){
			arg = arg || {}
			arg["콜백"] = arg["콜백"] || function(){}
			
			//ajax
			var serviceName = "reservEmailViewYN";
			var options = {
				"email": arg["이메일"]
			}
			_gf.ajax({
				"sid": "RS001"
				, "servicename": serviceName
			}, options, function(ret){
				var res = {
					"결과코드": ret["msp"]["contents"]["serviceResult"]
					, "메시지": _gf.getResultMsg(ret)
				}
				
				arg["콜백"].apply(null, [res]);
			});
		}
	}
	
	
	// 컨트롤
	function clsControl()
	{
		this.objComm = null;
		this.objEmailList = null;
		
		this._permitStart = null;
		this.openerControl = null;
	}
	
	clsControl.prototype = {
		_init: function(){
			this._preload();
			
			this.objComm = new clsComm();
			this.objEmailList = new clsEmailList();
			
			this._bind();
			this._beforeStart();
			
			_util.setWindowSize();
		}
		, _preload: function(){
			_gf.spinStart();
			
			if(opener && opener.document && opener.document.objControl) this.openerControl = opener.document.objControl;
		}
		, _bind: function(){
			var thisClass = this;
			
			// 메일리스트에서 삭제
			this.objEmailList.$getLayer().on("click", "img[alt='삭제']", function(){
				var n = thisClass.objEmailList.$getLayer().find("img[alt='삭제']").index($(this));
				var em = thisClass.objEmailList.getEmail(n);
				
				thisClass.emailDelete(em);
			});
			
			$(document).on("click", "img[alt='선택완료'],img[alt='close']", function(){
				var alt = $(this).attr("alt");
				switch(alt){
					case "선택완료":
						if(!thisClass.objEmailList.valid()){
							alert(thisClass.objEmailList.getError());
							return;
						}
						
						thisClass.emailSubmit();
						break;
					case "close":
						self.close();
				}
			});
		}
		, _beforeStart: function(){
			var thisClass = this;
			this._permitStart = {
				
			}
			
			_util.executeThread(function(){
				for(var k in thisClass._permitStart){
					if(!thisClass._permitStart[k]) return false;
				}
				
				return true
			}, function(){
				_gf.spinEnd();
				thisClass._start();
			});
		}
		, _start: function(){
			var thisClass = this;
			
			this.objEmailList.spinStart();
			// 이메일 리스트
			this.objComm.getEmailList({
				"콜백": function(ret){
					thisClass.finishEmailList(ret);
				}
			});
		}
		// 이메일 리스트 완료
		, finishEmailList: function(ret){
			if(ret["메시지"]) alert(ret["메시지"]);
			if(ret["결과코드"] != "00000") return;
			
			this.objEmailList.spinEnd();
			this.objEmailList.setList(ret["결과값"]);
		}
		// 이메일 삭제 완료
		, finishEmailDelete: function(ret){
			var thisClass = this;
			
			if(ret["메시지"]) alert(ret["메시지"]);
			if(ret["결과코드"] != "00000") return;
			
			// 이메일 리스트
			this.objComm.getEmailList({
				"콜백": function(ret){
					thisClass.finishEmailList(ret);
				}
			});
		}
		// 선택완료
		, emailSubmit: function(){
			this.openerControl.closeEmailList(self, this.objEmailList.getValue());
			self.close();
		}
		// 이메일 삭제
		, emailDelete: function(em){
			var thisClass = this;
			
			if(!confirm("삭제하시겠습니까?")) return;
			
			this.objEmailList.spinStart();
			this.objComm.emailDelete({
				"이메일": em
				, "콜백": function(ret){
					thisClass.finishEmailDelete(ret);
				}
			});
		}
	}
	
	document.objControl = new clsControl();
	document.objControl._init();
});